import {createSlice} from "@reduxjs/toolkit";
import {getOneEquipmentByIdAPI,getAllEquipmentByPageAPI} from "@/apis/equipment.jsx";

const equipmentStore=createSlice({
    name:'equipmentInfo',
    initialState:{
        equipment:{},
        equipmentList:[],
        equipmentFileList:[]
    },
//     修改数据的同步方法
    reducers:{
    //     1.根据id获取一条设备信息
        getOneEquipmentById(state,action){
            state.equipment=action.payload;
        },
    //     2.分页获取所有材料信息
        getAllEquipmentByPage(state,action){
            state.equipmentList=action.payload;
        }

    }
})

//解构出同步修改函数
const {getOneEquipmentById,getAllEquipmentByPage}=equipmentStore.actions;

//异步请求部分
// 根据id获取后端的一个equipment对象数据
const fetchGetOneEquipmentById=(id)=>{
    return async  (dispatch)=>{
        const res=await getOneEquipmentByIdAPI(id);
        dispatch(getOneEquipmentById(res.data))
    }
}

// 分页获取后端的equipment对象列表
const fetchGetEquipmentByPage=(current,pageSize)=>{
    return async (dispatch)=>{
        const res=await getAllEquipmentByPageAPI(current,pageSize);
        dispatch(getAllEquipmentByPage(res.data))
    }
}

const equipmentInfoReducer=equipmentStore.reducer;

export {fetchGetOneEquipmentById,fetchGetEquipmentByPage}
export default equipmentInfoReducer;
